package com.hua.demo.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hua.demo.edu.entity.Course;
import com.hua.demo.edu.entity.CourseDescription;
import com.hua.demo.edu.entity.frontvo.CourseFrontVo;
import com.hua.demo.edu.entity.vo.CourseInfoVo;
import com.hua.demo.edu.entity.vo.CoursePublishVo;
import com.hua.demo.edu.mapper.CourseMapper;
import com.hua.demo.edu.service.CourseDescriptionService;
import com.hua.demo.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author huazi
 * @since 2021-11-24
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Override
    public void savecourseinfo(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        boolean save = this.save(course);
        if(save==false){
            throw new GuliException(20001,"添加失败");
        }
        String id = course.getId();
        CourseDescription courseDescription = new CourseDescription();

        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        courseDescription.setId(id);
        courseDescriptionService.save(courseDescription);

    }

    @Override
    public CourseInfoVo getcoursebyid(String courseid) {
        Course byId = this.getById(courseid);
        CourseDescription byId1 = courseDescriptionService.getById(courseid);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(byId,courseInfoVo);
        courseInfoVo.setDescription(byId1.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updatecourseinfo(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        boolean update = this.updateById(course);
        if(update==false){
            throw new GuliException(20001,"更新失败");
        }
        String id = course.getId();
        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        courseDescription.setId(id);
        courseDescriptionService.updateById(courseDescription);

    }

    @Override
    public CoursePublishVo getcoursepublicvo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;

    }

    @Override
    //前端
    public Map<String, Object> getfrontcourselist(Page<Course> pageParam, CourseFrontVo courseFrontVo) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(pageParam, wrapper);

        List<Course> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页
        System.out.println(current+"+"+pages+"+"+size+"+"+total+"+"+hasNext);

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;


    }
}
